package com.yunhe.tomcat.student;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yunhe.tomcat.iservice.IStudentTurnDao;
import com.yunhe.tomcat.iservice.impl.StudentTurnImpl;
import com.yunhe.tomcat.model.Student;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class DeleteServlet
 */
@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 设置请求以及响应的内容类型以及编码方式
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");

        // 1.实例化接口实现类
        IStudentTurnDao stu = new StudentTurnImpl();
        // 2.返回运行结果
        int record = stu.deleteStudent(Integer.parseInt(request.getParameter("studentId")));

        // 3.将int对象转换成json对象
        JSONArray json = JSONArray.fromObject(record);
        // 4.将json对象转换成字符串
        String str = json.toString();
        // 5.向客户端发送字符串
        PrintWriter out = response.getWriter();
        out.write(str); // 将str发送到前台

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
